interface GoodsItem {
  id: string
  name: string
  description: string
  price: number
  image: string
  seller: string
  tags: string[]
  badge?: string
}

Page({
  data: {
    onlineUsers: 128,
    todayItems: 45,
    searchKeyword: '',
    currentFilter: 'all',
    goodsList: [] as GoodsItem[],
    hasMore: true,
    loading: false,
    page: 1
  },

  onLoad() {
    console.log('好物剪影页面加载');
    this.fetchGoodsList()
  },

  onShow() {
    
  },

  // 获取商品列表
  fetchGoodsList(refresh = false) {
    if (this.data.loading) return
    
    this.setData({ loading: true })
    
    if (refresh) {
      this.setData({ page: 1, goodsList: [] })
    }
    
    // 模拟API调用
    setTimeout(() => {
      const mockItems: GoodsItem[] = [
        {
          id: '1',
          name: '稀有武器 - 龙牙剑',
          description: '传说级武器，攻击力极高，适合高级战士使用',
          price: 1500,
          image: 'https://via.placeholder.com/400x300/FFD700/000000?text=龙牙剑',
          seller: '战士之王',
          tags: ['武器', '传说级', '战士专用'],
          badge: '热门'
        },
        {
          id: '2',
          name: '魔法护符 - 守护之心',
          description: '提供强大的防御加成，增加魔法抗性',
          price: 800,
          image: 'https://via.placeholder.com/400x300/87CEEB/000000?text=守护之心',
          seller: '魔法师',
          tags: ['护符', '防御', '魔法抗性']
        },
        {
          id: '3',
          name: '稀有宠物 - 火焰凤凰',
          description: '传说中的神兽，拥有强大技能和华丽外观',
          price: 2500,
          image: 'https://via.placeholder.com/400x300/FF6347/FFFFFF?text=火焰凤凰',
          seller: '宠物专家',
          tags: ['宠物', '神兽', '火焰系'],
          badge: '稀有'
        },
        {
          id: '4',
          name: '装备套装 - 暗影刺客',
          description: '完整的刺客装备套装，提供隐身和暴击加成',
          price: 1200,
          image: 'https://via.placeholder.com/400x300/696969/FFFFFF?text=暗影刺客',
          seller: '刺客大师',
          tags: ['套装', '刺客', '隐身']
        },
        {
          id: '5',
          name: '魔法杖 - 星辰之杖',
          description: '镶嵌星辰宝石的法杖，增强魔法威力',
          price: 1800,
          image: 'https://via.placeholder.com/400x300/9370DB/FFFFFF?text=星辰之杖',
          seller: '星辰法师',
          tags: ['法杖', '魔法', '星辰系'],
          badge: '新品'
        },
        {
          id: '6',
          name: '防具 - 龙鳞铠甲',
          description: '用龙鳞打造的铠甲，提供极高的物理防御',
          price: 2200,
          image: 'https://via.placeholder.com/400x300/8B4513/FFFFFF?text=龙鳞铠甲',
          seller: '铁匠大师',
          tags: ['防具', '铠甲', '龙鳞']
        }
      ]
      
      const newList = refresh ? mockItems : [...this.data.goodsList, ...mockItems]
      
      this.setData({
        goodsList: newList,
        loading: false,
        hasMore: this.data.page < 3, // 模拟只有3页数据
        page: this.data.page + 1
      })
    }, 1000)
  },

  // 搜索输入
  onSearchInput(e: any) {
    this.setData({
      searchKeyword: e.detail.value
    })
  },

  // 执行搜索
  onSearch() {
    wx.showToast({
      title: `搜索: ${this.data.searchKeyword}`,
      icon: 'none',
      duration: 2000
    })
    // 这里可以调用搜索API
  },

  // 设置筛选
  setFilter(e: any) {
    const filter = e.currentTarget.dataset.filter
    this.setData({
      currentFilter: filter
    })
    
    wx.showToast({
      title: `筛选: ${filter}`,
      icon: 'none',
      duration: 1500
    })
    
    // 重新加载数据
    this.fetchGoodsList(true)
  },

  // 跳转到商品详情
  goToGoodsDetail(e: any) {
    const item = e.currentTarget.dataset.item
    wx.showToast({
      title: `查看${item.name}`,
      icon: 'none',
      duration: 2000
    })
  },

  // 加载更多
  loadMore() {
    if (this.data.hasMore && !this.data.loading) {
      this.fetchGoodsList()
    }
  },

  // 显示发布弹窗
  showPublishModal() {
    wx.showModal({
      title: '发布商品',
      content: '发布商品功能开发中，敬请期待！',
      showCancel: false,
      confirmText: '知道了'
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.fetchGoodsList(true)
    wx.stopPullDownRefresh()
  },

  // 上拉加载更多
  onReachBottom() {
    this.loadMore()
  }
}); 